#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Created on 2018-10-20 15:19:16
# Project: m_autohome_com_cn

import json
import html
import time
import random
import string
from pyspider.libs.base_handler import *
from pyquery import PyQuery as pq

from src.mysqldb import SQL
from src.headers_switch import HeadersSelector
from src.utils import download_img
from src.utils import md5
from src.utils import oss

class Handler(BaseHandler):
    crawl_config = {
        'itag': 'v0.0.9'
    }

    @every(minutes=500)
    def on_start(self):
        header_slt = HeadersSelector()
        header = header_slt.select_header()  # 获取一个新的 header
        #self.crawl('m.jd.com', callback=self.index_page)
        header['Referer'] = 'https://m.autohome.com.cn/'
        gid = '28393'
        goods = {
            'gid': gid,
            'header': header,
        }
        url = 'https://m.autohome.com.cn/spec/%s/' % (gid,)
        self.crawl(url, callback=self.spec_page, headers=header, save=goods)
        return
        goods_ids = [
            '21776',
            '23012',
            '23358',
            '24733',
            '25892',
            '26547',
            '27310',
            '28393',
            '29048',
            '29316',
            '29493',
            '30105',
            '30975',
            '31040',
            '31053',
            '31058',
            '31464',
            '31477',
            '31523',
            '31594',
            '31841',
            '31921',
            '32015',
            '32064',
            '32165',
            '32168',
            '32190',
            '32334',
            '32456',
            '32464',
            '32537',
            '32567',
            '32661',
            '32700',
            '33034',
            '33068',
            '33211',
            '33259',
            '33279',
            '33304',
            '33307',
            '33320',
            '33339',
            '33361',
            '33568',
            '33595',
            '33655',
            '33853',
            '33896',
            '33998',
            '33999',
            '34033',
            '34127',
            '34169',
            '34196',
            '34203',
            '34276',
            '34349',
            '34407',
            '34554',
            '34567',
            '34600',
            '34758',
            '34764',
            '1002001',
            '1003454',
        ]
        for gid in goods_ids:
            # time.sleep(random.randint(50, 60))
            url = 'https://m.autohome.com.cn/spec/%s/' % (gid, )
            goods = {
                'gid': gid,
                'header': header,
            }
            self.crawl(url, callback=self.spec_page, headers=header, save=goods)

    @config(priority=2)
    def spec_page(self, response):
        """
        综述
        """
        doc = response.doc
        goods = response.save
        gid = goods.get('gid', 0)
        header = goods.get('header', {})

        goods['guide_price'] = doc('.guide strong').text()

        goods['praise_score'] = doc('.grade .data').text()
        goods['praise_score_num'] = doc('.grade small').text()
        goods['oil_wear_avg'] = doc('.wear .data').text()
        goods['oil_wear_num'] = doc('.wear small').text()

        # 外观
        url = 'https://car.m.autohome.com.cn/pic/car/%s-1-0-i0.html' % (gid,)
        self.crawl(url, callback=self.img_exterior_page, headers=header, save=goods)

    @config(priority=3)
    def img_exterior_page(self, response):
        """ 外观 """
        doc = response.doc
        goods = response.save
        gid = goods.get('gid', '0')
        header = goods.get('header', {})

        img_li = doc('#listPic .three-columns li a img').items()
        img_li = [img.attr('src').replace('280x210', '1280x960') for img in img_li]

        subpath = 'goods_car/gid_%s/%s' % (gid, 'exterior')
        img_li = [oss(subpath, img, headers=header) for img in img_li if img is not None]

        goods['img_exterior'] = json.dumps(img_li)
        # 中控方向盘
        url = 'https://car.m.autohome.com.cn/pic/car/%s-10-0-i0.html' % (gid,)
        self.crawl(url, callback=self.img_wheel_page, headers=header, save=goods)

    @config(priority=3)
    def img_wheel_page(self, response):
        """ 中控方向盘 """
        doc = response.doc
        goods = response.save
        gid = goods.get('gid', '0')
        header = goods.get('header', {})

        img_li = doc('#listPic .three-columns li a img').items()
        img_li = [img.attr('src').replace('280x210', '1280x960') for img in img_li]

        subpath = 'goods_car/gid_%s/%s' % (gid, 'wheel')
        img_li = [oss(subpath, img, headers=header) for img in img_li if img is not None]

        goods['img_wheel'] = json.dumps(img_li)
        # 中控方向盘
        url = 'https://car.m.autohome.com.cn/pic/car/%s-3-0-i0.html' % (gid,)
        self.crawl(url, callback=self.img_seat_page, headers=header, save=goods)

    @config(priority=3)
    def img_seat_page(self, response):
        """ 车厢座椅 """
        doc = response.doc
        goods = response.save
        gid = goods.get('gid', '0')
        header = goods.get('header', {})

        img_li = doc('#listPic .three-columns li a img').items()
        img_li = [img.attr('src').replace('280x210', '1280x960') for img in img_li]

        subpath = 'goods_car/gid_%s/%s' % (gid, 'seat')
        img_li = [oss(subpath, img, headers=header) for img in img_li if img is not None]

        goods['img_seat'] = json.dumps(img_li)
        # 其他细节
        url = 'https://car.m.autohome.com.cn/pic/car/%s-12-0-i0.html' % (gid,)
        self.crawl(url, callback=self.img_other_page, headers=header, save=goods)

    @config(priority=3)
    def img_other_page(self, response):
        """ 其他细节 """
        doc = response.doc
        goods = response.save
        gid = goods.get('gid', '0')
        header = goods.get('header', {})

        img_li = doc('#listPic .three-columns li a img').items()
        img_li = [img.attr('src').replace('280x210', '1280x960') for img in img_li]

        subpath = 'goods_car/gid_%s/%s' % (gid, 'other')
        img_li = [oss(subpath, img, headers=header) for img in img_li if img is not None]

        goods['img_other'] = json.dumps(img_li)
        # 车型配置
        url = 'https://car.m.autohome.com.cn/config/spec/%s.html' % (gid,)
        self.crawl(url, callback=self.config_page, headers=header, save=goods, fetch_type='js', js_script="""function() { setTimeout("var a=1;", 300);}""",)

    @config(priority=3)
    def config_page(self, response):
        # time.sleep(random.randint(5, 6))
        doc = response.doc
        goods = response.save
        gid = goods.get('gid', '0')
        header = goods.get('header', {})

        goods['specification'] = html.escape(doc('.detail').html())
        url = 'https://jiage.m.autohome.com.cn/mobile/carlist/p-%s' % (gid,)
        self.crawl(url, callback=self.price_page, headers=header, save=goods)

    @config(priority=3)
    def price_page(self, response):
        # time.sleep(random.randint(5, 6))
        doc = response.doc
        goods = response.save
        gid = goods.get('gid', '0')
        header = goods.get('header', {})

        items = list(doc('.cz-bare .transfer .c-orange').items())
        goods['naked_car_price'] = items[0].text()
        goods['full_paid_price'] = items[1].text()
        url = 'https://k.m.autohome.com.cn/spec/%s/' % (gid,)
        self.crawl(url, callback=self.public_praise_page, headers=header, save=goods, fetch_type='js', js_script="""function() { setTimeout("$('.more').click()", 1000); $('#chart').attr('src', document.getElementById("chart").toDataURL("image/png")); }""")
        # self.crawl(url, callback=self.public_praise_page, headers=header, save=goods)

    def public_praise_page(self, response):
        # time.sleep(random.randint(5, 6))
        doc = response.doc
        goods = response.save
        gid = goods.get('gid', '0')
        header = goods.get('header', {})

        subpath = 'goods_car/gid_%s/%s' % (gid, 'histogram')
        img = doc('#chart').attr('src')
        goods['histogram'] = oss(subpath, img, headers=header)
        #goods['histogram'] = doc('#chart').attr('src')

        grade = [{'id':item.attr('value'), 'name':item.text()} for item in doc('#js_conditionParent .anchor span').items()]
        goods['grade'] = json.dumps(grade)

        tag = [{'id':item.attr('value'), 'name':item.text().rstrip(string.digits), 'num': item.children('span').text()} for item in doc('#second0 a').items()]
        goods['tag'] = json.dumps(tag)

        goods['comments'] = json.dumps(['https:%s' % comm.attr('onclick')[26:-1] for comm in doc('.comments ul').items()])
        return goods

    def on_result(self,result):
        # print(result)
        if not result or not result.get('gid', ''):
            return
        sql = SQL()
        where = "`id`='%s'" % (result['gid'], )
        result.pop('gid')
        header = result.get('header', None)
        if header:
            result.pop('header')
        sql.update('jjb_goods_car', where, result)
